package com.eastfair.venuebooking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.venuebooking.entity.ContractManage;
import com.eastfair.venuebooking.entity.Exhibition;
import com.eastfair.venuebooking.exceptioncode.VenueBookingExceptionCode;
import com.eastfair.venuebooking.service.ContractManageService;
import com.eastfair.venuebooking.service.ExhibitionService;
import com.eastfair.venuebooking.service.PayBusinessService;
import com.eastfair.venuebooking.vo.ContractManageVO;
import com.eastfair.venuebooking.vo.ContractSettlementVO;
import com.eastfair.venuebooking.vo.SettlementTotalVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class PayBusinessServiceImpl implements PayBusinessService {
    @Autowired
    private ExhibitionService exhibitionService;
    @Autowired
    private ContractManageService contractService;

    /**
     * 结算查询
     * @param exhibitionId
     * @return
     */
    @Override
    public SettlementTotalVO settlementQuery(Long exhibitionId) {
        //查询条件
        QueryWrapper<ContractManage> contractLambdaQueryWrapper = new QueryWrapper<>();
        contractLambdaQueryWrapper.lambda().eq(ContractManage::getExhibitionId,exhibitionId);
        contractLambdaQueryWrapper.lambda().eq(ContractManage::getIsDeleted,0);
        //查询展会与合同
        List<ContractManage> contractList = contractService.list(contractLambdaQueryWrapper);
        //循环计算结算金额以及列表项
        SettlementTotalVO settlementTotalVO = new SettlementTotalVO();
        //组装数据
        List<ContractSettlementVO> contractSettlementVOList = new ArrayList<>();
        for (ContractManage c:contractList) {
            ContractSettlementVO  contractSettlementVO = new ContractSettlementVO();
            //实体copy
            BeanUtils.copyProperties(c,contractSettlementVO);
            //结算金额
            contractSettlementVO.setSettlementAmout((c.getContractAmount()==null?new BigDecimal(0):c.getContractAmount()).subtract((c.getContractReturndAmount()==null?new BigDecimal(0):c.getContractReturndAmount())).setScale(2,BigDecimal.ROUND_HALF_UP));
            contractSettlementVOList.add(contractSettlementVO);
            //合计
            settlementTotalVO.setTotal((settlementTotalVO.getTotal()==null?new BigDecimal(0):settlementTotalVO.getTotal()).add((c.getContractAmount()==null?new BigDecimal(0):c.getContractAmount())).setScale(2,BigDecimal.ROUND_HALF_UP));
            //场地保证金
            settlementTotalVO.setSpaceBondTotal((settlementTotalVO.getSpaceBondTotal()==null?new BigDecimal(0):settlementTotalVO.getSpaceBondTotal()).add((c.getSpaceBond()==null?new BigDecimal(0):c.getSpaceBond())).setScale(2,BigDecimal.ROUND_HALF_UP));
            //预存款项
            settlementTotalVO.setServiceBondTotal((settlementTotalVO.getSpaceBondTotal()==null?new BigDecimal(0):settlementTotalVO.getSpaceBondTotal()).add((c.getServiceBond()==null?new BigDecimal(0):c.getServiceBond()).setScale(2,BigDecimal.ROUND_HALF_UP)));
            //结算实付金额
            settlementTotalVO.setSettlementActualAmoutToatal((settlementTotalVO.getSettlementActualAmoutToatal()==null?new BigDecimal(0):settlementTotalVO.getSettlementActualAmoutToatal()).add((c.getContractReturndAmount()==null?new BigDecimal(0):c.getContractReturndAmount())).setScale(2,BigDecimal.ROUND_HALF_UP));
            //已开票金额
            settlementTotalVO.setAlreadyInvoicingAmout((settlementTotalVO.getAlreadyInvoicingAmout()==null?new BigDecimal(0):settlementTotalVO.getAlreadyInvoicingAmout()).add((c.getInvoicedAmount()==null?new BigDecimal(0):c.getInvoicedAmount())).setScale(2,BigDecimal.ROUND_HALF_UP));

        }
        //结算应付金额
        settlementTotalVO.setSettlementAmoutToatal(settlementTotalVO.getTotal()==null?new BigDecimal(0):settlementTotalVO.getTotal());
        //结算应退金额
        settlementTotalVO.setSettlementRetreatAmoutToatal((settlementTotalVO.getTotal()==null?new BigDecimal(0):settlementTotalVO.getTotal()).subtract((settlementTotalVO.getSettlementActualAmoutToatal()==null?new BigDecimal(0):settlementTotalVO.getSettlementActualAmoutToatal())).setScale(2,BigDecimal.ROUND_HALF_UP));
        //未开票金额
        settlementTotalVO.setNotInvoicingAmout((settlementTotalVO.getTotal()==null?new BigDecimal(0):settlementTotalVO.getTotal()).subtract((settlementTotalVO.getAlreadyInvoicingAmout()==null?new BigDecimal(0):settlementTotalVO.getAlreadyInvoicingAmout())).setScale(2,BigDecimal.ROUND_HALF_UP));
        //组装数据
        settlementTotalVO.setContractSettlementVOList(contractSettlementVOList);

        //查询展会信息
        Exhibition exhibition = exhibitionService.getById(exhibitionId);
        //数据复制
        BeanUtils.copyProperties(exhibition,settlementTotalVO);
        settlementTotalVO.setExhibitionId(exhibition.getId());
       return  settlementTotalVO ;
    }
    /**
     * 申请开票前置查询、根据合同ID查询
     */
    @Override
    public ContractManageVO queryContractManageById(Long contractManageId) {
        ContractManage contractManage= contractService.getById(contractManageId);
        ContractManageVO contractManageVO = new ContractManageVO();
        BeanUtils.copyProperties(contractManage,contractManageVO);
        contractManageVO.setId(contractManageId);
        contractManageVO.setSureInvoicedAmount(contractManage.getContractReturndAmount().subtract(contractManage.getInvoicedAmount()).setScale(2,BigDecimal.ROUND_HALF_UP));
        return contractManageVO;
    }
    /**
     * 修改合同开票金额
     */
    @Override
    public void updateContractManageById(Long contractManageId) {
        //查询合同是否存在
        ContractManage contractManage= contractService.getById(contractManageId);
        if(null == contractManage){
            throw  new BizException(VenueBookingExceptionCode.CONTRACT_DOES_NOT_EXIST.getMsg());
        }
        //设置开票金额、目前是全部开票
        contractManage.setInvoicedAmount((contractManage.getInvoicedAmount()== null?new BigDecimal(0):contractManage.getInvoicedAmount()).add(Objects.isNull(contractManage.getContractReturndAmount())? new BigDecimal(0):contractManage.getContractReturndAmount()));
        //修改合同开票金额
        contractService.updateById(contractManage);
    }
}
